package Main;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class Main extends JFrame implements ActionListener {
	private JPanel panel1, panel2;
	private JLabel total_t, pass_t, rem_t, h, m, sc, setup;
	private static JTextField text_tt, text_st, text_rt, text_t, text_h, text_m, text_sc;
	private Font fontWord, fontNo, fontTime, fontBtnWord;
	private JButton start, pause, continu, conceal, reveal, exit;
	private static boolean flag = true;
	private static long s, t;//s是变动的剩余时间，t是不变的总时间
	
	public Main() {
		super("计时器");
		setSize(800, 600);
		setLocation(100, 100);
		setLayout(null);    //清空布局管理器
		setResizable(false); //固定大小
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //退出程序
		
		/*-----Panel1中组件初始化-----*/
		panel1 = new JPanel();
		setup = new JLabel("倒计时时间设置：");
		h = new JLabel("时");
		m = new JLabel("分");
		sc = new JLabel("秒");
		text_h = new JTextField(10);
		text_h.setText("00");
		text_m = new JTextField(10);
		text_m.setText("00");
		text_sc = new JTextField(10);
		text_sc.setText("00");
		start = new JButton("开始");
		pause = new JButton("暂停");
		continu = new JButton("继续");
		conceal = new JButton("隐藏");
		reveal = new JButton("显示");
		exit = new JButton("退出");
		
		/*-----panel2中组件初始化-----*/
		panel2 = new JPanel();
		total_t = new JLabel("计时总秒数：");
		pass_t = new JLabel("已过秒数：");
		rem_t = new JLabel("剩余秒数：");
		text_tt = new JTextField(30);
		text_rt = new JTextField(30);
		text_st = new JTextField(30);
		text_t = new JTextField(30);
		
		/*-----字体样式初始化-----*/
		fontWord = new Font("黑体", 0, 20);  	  //无框的文字的格式
		fontBtnWord = new Font("黑体", 0, 16); //按钮内文字的格式
		fontNo = new Font("黑体", 0, 20);      //框内数字的格式
		fontTime = new Font("黑体", 0, 85); 	 //倒计时显示的格式
		
		/*---panel1（上半区）的设置---*/
		panel1.setBounds(0, 0, 800, 200);
		panel1.setLayout(null);   //清空布局管理器
		
		//左上角的设置
		setup.setBounds(50, 40, 200, 40);
		setup.setFont(fontWord);//文字格式设置
		setup.setForeground(Color.RED);//文字颜色设置
		
		h.setBounds(120, 75, 120, 40);
		h.setFont(fontWord);
		
		m.setBounds(200, 75, 120, 40);
		m.setFont(fontWord);
		
		sc.setBounds(280, 75, 120, 40);
		sc.setFont(fontWord);
		
		text_h.setBounds(70, 80, 50, 30);
		text_h.setBackground(Color.WHITE);//背景颜色设置
		text_h.setFont(fontNo);
		text_h.setHorizontalAlignment(JTextField.RIGHT);//文字所处位置设置（靠右对齐）
		
		text_m.setBounds(150, 80, 50, 30);
		text_m.setBackground(Color.WHITE);
		text_m.setFont(fontNo);
		text_m.setHorizontalAlignment(JTextField.RIGHT);
		
		text_sc.setBounds(230, 80, 50, 30);
		text_sc.setBackground(Color.WHITE);
		text_sc.setFont(fontNo);
		text_sc.setHorizontalAlignment(JTextField.RIGHT);
		
		//右上角按钮的设置
		start.setBounds(400, 50, 110, 40);
		start.setFont(fontBtnWord);
		
		pause.setBounds(520, 50, 110, 40);
		pause.setFont(fontBtnWord);
		
		continu.setBounds(640, 50, 110, 40);
		continu.setFont(fontBtnWord);
		
		conceal.setBounds(400, 95, 110, 40);
		conceal.setFont(fontBtnWord);
		
		reveal.setBounds(520, 95, 110, 40);
		reveal.setFont(fontBtnWord);
		
		exit.setBounds(640, 95, 110, 40);
		exit.setFont(fontBtnWord);
		
		
		/*---panel2的设置---*/
		panel2.setBounds(0, 200, 800, 400);
		panel2.setLayout(null);
		panel2.setBackground(Color.WHITE);
		
		//文字的设置
		total_t.setBounds(10, 10, 120, 40);
		total_t.setFont(fontWord);
		
		pass_t.setBounds(270, 10, 120, 40);
		pass_t.setFont(fontWord);
		
		rem_t.setBounds(510, 10, 120, 40);
		rem_t.setFont(fontWord);
		
		//框内数字的设置
		text_tt.setBounds(140, 15, 120, 30);
		text_tt.setBackground(Color.WHITE);
		text_tt.setForeground(Color.BLUE);
		text_tt.setEditable(false); //禁止在框内编辑
		text_tt.setFont(fontNo);
		
		text_st.setBounds(380, 15, 120, 30);
		text_st.setBackground(Color.WHITE);
		text_st.setForeground(Color.BLUE);
		text_st.setEditable(false);
		text_st.setFont(fontNo);
		
		text_rt.setBounds(620, 15, 120, 30);
		text_rt.setBackground(Color.WHITE);
		text_rt.setForeground(Color.RED);
		text_rt.setEditable(false);
		text_rt.setFont(fontNo);
		
		text_t.setBounds(10, 70, 760, 270);
		text_t.setBackground(Color.BLACK);
		text_t.setForeground(Color.RED);
		text_t.setEditable(false);
		text_t.setFont(fontTime);
		text_t.setHorizontalAlignment(JTextField.CENTER);
		
		/*-----将各组件添加至panel1中-----*/
		panel1.add(setup);
		panel1.add(h);
		panel1.add(m);
		panel1.add(sc);
		panel1.add(text_h);
		panel1.add(text_m);
		panel1.add(text_sc);
		panel1.add(start);
		panel1.add(pause);
		panel1.add(continu);
		panel1.add(conceal);
		panel1.add(reveal);
		panel1.add(exit);
		
		/*-----剩余组件添加至panel2中-----*/
		panel2.add(total_t);
		panel2.add(pass_t);
		panel2.add(rem_t);
		panel2.add(text_tt);
		panel2.add(text_st);
		panel2.add(text_rt);
		panel2.add(text_t);
		
		/*-----在主界面中添加panel1和panel2-----*/
		add(panel1);
		add(panel2);
		
		/*-----添加按钮监听器-----*/
		start.addActionListener(new S_ActionListener());
		pause.addActionListener(new P_ActionListener());
		continu.addActionListener(new C_ActionListener());
		conceal.addActionListener(new Co_ActionListener());
		reveal.addActionListener(new R_ActionListener());
		exit.addActionListener(new E_ActionListener());
	}
		
	
	
    public static void main(String[] args) {
    	Main timer = new Main();
		timer.setVisible(true);//显示按钮
		Timer te = new Timer();
		te.start();//线程开始运行
    }
    
    /*-----线程-----*/
    static class Timer extends Thread{
	    public void run() {
	    	while (true) {
	    		if (s > -1) {
	    			if (flag) {
	    				long h = s / 36000;//小时
				    	long m = (s - h * 36000) / 600;//分钟
				    	long sc = s / 10 - h * 3600 - m * 60;//秒
				    	text_tt.setText(String.valueOf(t / 10));   //转换成String格式
				    	text_st.setText(String.valueOf(t / 10 - s / 10));
				    	text_rt.setText(String.valueOf(s / 10));
				    	text_t.setText(String.valueOf(h) + " : " + String.valueOf(m) + " : " + 
				    			String.valueOf(sc));
				    	if (s > 0) s--;//剩余秒数-1
				    }              //每经过0.1秒，s减少1，s是秒乘10之后的数字，所以过一秒，s-10，实际显示的剩余秒数-1
				    try {
				    	Thread.sleep(100);   //程序暂停100毫秒（就是刷新秒数），就是每0.1秒刷新一次，提高响应速度。
				    } catch (InterruptedException e1) {
				    	e1.printStackTrace();
				    }
	    		}
			}
	    }
	}
    
    /*-----开始按钮事件监听器-----*/
    private class S_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				if (text_h.getText().equals("")) 
					text_h.setText("00");		//防止时间设置中内容为空，默认为0
				if (text_m.getText().equals("")) 
					text_m.setText("00");
				if (text_sc.getText().equals("")) 
					text_sc.setText("00");
				int h = Integer.parseInt(text_h.getText());
				int m = Integer.parseInt(text_m.getText());
				int sc = Integer.parseInt(text_sc.getText());
				if(h < 0 || m < 0 || sc < 0 || h > 9999)
					throw new IllegalArgumentException();//抛出异常，以防止时间设置超出范围
				if(m > 60 || sc > 60)//防止分秒超出60
					JOptionPane.showMessageDialog(Main.this, "分秒不能超过60！", "错误", JOptionPane.ERROR_MESSAGE);
				else {
					flag = true;//使计时开始
					/*-----重新设定计时-----*/
					s = h * 36000 + m * 600 + sc * 10 + 9;
					t = s;
				}
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(Main.this, "时间必须为整数！", "错误", JOptionPane.ERROR_MESSAGE);
			} catch (IllegalArgumentException e1) {
				JOptionPane.showMessageDialog(Main.this, "小时只能取0-9999，分秒只可取0-60！", "错误", JOptionPane.ERROR_MESSAGE);
			}
		}
    }
    
    /*-----暂停按钮事件监听器-----*/
    private class P_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			flag = false;//计时暂停
		}
    }
    
    /*-----继续按钮事件监听器-----*/
    private class C_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			flag = true;//计时继续
		}
    }
    
    /*-----隐藏按钮事件监听器-----*/
    private class Co_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			panel2.setVisible(false);//隐藏panel2界面
			setSize(800, 235);//重设主界面大小，防止出现空白区
		}
    }
    
    /*-----显示按钮事件监听器-----*/
    private class R_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			panel2.setVisible(true);//显示panel2界面
			setSize(800, 600);//将主界面设置回原来的样子
		}
    }
    
    /*-----退出按钮事件监听器-----*/
    private class E_ActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();//释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源
		}
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
}
